package com.ruoyi.project.system.baseinfo.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.record.domain.TDiseaseRecord;
import com.ruoyi.project.system.record.service.ITDiseaseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.baseinfo.mapper.TNewbornBaseinfoMapper;
import com.ruoyi.project.system.baseinfo.domain.TNewbornBaseinfo;
import com.ruoyi.project.system.baseinfo.service.ITNewbornBaseinfoService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 新生儿基本信息Service业务层处理
 *
 * @author
 * @date 2021-03-31
 */
@Service
public class TNewbornBaseinfoServiceImpl implements ITNewbornBaseinfoService
{
    @Autowired
    private TNewbornBaseinfoMapper tNewbornBaseinfoMapper;

    @Autowired
    private ITDiseaseRecordService itDiseaseRecordService;

    /**
     * 查询新生儿基本信息
     *
     * @param id 新生儿基本信息ID
     * @return 新生儿基本信息
     */
    @Override
    public TNewbornBaseinfo selectTNewbornBaseinfoById(Long id)
    {
        return tNewbornBaseinfoMapper.selectTNewbornBaseinfoById(id);
    }

    /**
     * 查询新生儿基本信息列表
     *
     * @param tNewbornBaseinfo 新生儿基本信息
     * @return 新生儿基本信息
     */
    @Override
    public List<TNewbornBaseinfo> selectTNewbornBaseinfoList(TNewbornBaseinfo tNewbornBaseinfo)
    {
        return tNewbornBaseinfoMapper.selectTNewbornBaseinfoList(tNewbornBaseinfo);
    }

    /**
     * 新增新生儿基本信息
     *
     * @param tNewbornBaseinfo 新生儿基本信息
     * @return 结果
     */
    @Override
    public int insertTNewbornBaseinfo(TNewbornBaseinfo tNewbornBaseinfo)
    {
        tNewbornBaseinfo.setCreateTime(DateUtils.getNowDate());
        return tNewbornBaseinfoMapper.insertTNewbornBaseinfo(tNewbornBaseinfo);
    }

    /**
     * 修改新生儿基本信息
     *
     * @param tNewbornBaseinfo 新生儿基本信息
     * @return 结果
     */
    @Override
    public int updateTNewbornBaseinfo(TNewbornBaseinfo tNewbornBaseinfo)
    {
        tNewbornBaseinfo.setUpdateTime(DateUtils.getNowDate());
        return tNewbornBaseinfoMapper.updateTNewbornBaseinfo(tNewbornBaseinfo);
    }

    /**
     * 删除新生儿基本信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTNewbornBaseinfoByIds(String ids)
    {
        return tNewbornBaseinfoMapper.deleteTNewbornBaseinfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除新生儿基本信息信息
     *
     * @param id 新生儿基本信息ID
     * @return 结果
     */
    @Override
    public int deleteTNewbornBaseinfoById(Long id)
    {
        return tNewbornBaseinfoMapper.deleteTNewbornBaseinfoById(id);
    }

    /**
     * 修改保存新生儿初检信息
     * @param tNewbornBaseinfo
     * @return
     */
    @Override
    public int updateTNewbornFirstInspect(TNewbornBaseinfo tNewbornBaseinfo) {
        tNewbornBaseinfo.setUpdateTime(new Date());
        if(tNewbornBaseinfo.getStatus() == 2){
            // 待复检
        }else if(tNewbornBaseinfo.getStatus() == 3){
            // 待治疗
            if(StringUtils.isNotEmpty(tNewbornBaseinfo.getDiseaseName())){
                TDiseaseRecord tDiseaseRecord = new TDiseaseRecord();
                tDiseaseRecord.setDiseaseName(tNewbornBaseinfo.getDiseaseName());
                tDiseaseRecord.setStatus(1);
                tDiseaseRecord.setNewbornId(tNewbornBaseinfo.getId());
                itDiseaseRecordService.insertTDiseaseRecord(tDiseaseRecord);
            }
        }
        return tNewbornBaseinfoMapper.updateTNewbornBaseinfo(tNewbornBaseinfo);
    }

    /**
     * 查询新生儿复检基本信息列表
     * @param tNewbornBaseinfo
     * @return
     */
    @Override
    public List<TNewbornBaseinfo> selectTNewbornSecondInsepctList(TNewbornBaseinfo tNewbornBaseinfo) {
        return tNewbornBaseinfoMapper.selectTNewbornSecondInsepctList(tNewbornBaseinfo);
    }

    /**
     * 新生儿复检
     * @param tNewbornBaseinfo
     * @return
     */
    @Override
    public int updateTNewbornSecondInspect(TNewbornBaseinfo tNewbornBaseinfo) {
        tNewbornBaseinfo.setUpdateTime(new Date());
        if(tNewbornBaseinfo.getStatus() == 6){
            // 检查结束
        }else if(tNewbornBaseinfo.getStatus() == 3){
            // 待治疗
            if(StringUtils.isNotEmpty(tNewbornBaseinfo.getDiseaseName())){
                TDiseaseRecord tDiseaseRecord = new TDiseaseRecord();
                tDiseaseRecord.setDiseaseName(tNewbornBaseinfo.getDiseaseName());
                tDiseaseRecord.setStatus(1);
                tDiseaseRecord.setNewbornId(tNewbornBaseinfo.getId());
                itDiseaseRecordService.insertTDiseaseRecord(tDiseaseRecord);
            }
        }
        return tNewbornBaseinfoMapper.updateTNewbornBaseinfo(tNewbornBaseinfo);
    }
}
